package com.yyoo.doc.mypdf.builder;

import com.lowagie.text.*;
import com.yyoo.doc.html.style.StyleAttr;
import com.yyoo.doc.html.tag.HtmlTag;

import java.util.HashMap;
import java.util.Map;

/**
 * 解析 ul 或 ol 标签
 *
 * @author zzy
 */
public class PdfListBuilder extends PdfElementBuilder {

    static final Map<String,String> listTypeMap = new HashMap<>();
    static final Map<String,String> numberTypeMap = new HashMap<>();

    static {
        listTypeMap.put("●","●");
        listTypeMap.put("disc","●");
        listTypeMap.put("○","○");
        listTypeMap.put("circle","○");
        listTypeMap.put("■","■");
        listTypeMap.put("square","■");
        listTypeMap.put("□","□");

        numberTypeMap.put("1","1");
        numberTypeMap.put("i","i");
        numberTypeMap.put("I","I");
        numberTypeMap.put("a","a");
        numberTypeMap.put("A","A");
        numberTypeMap.put("α","α");
    }

    @Override
    protected void initDefaultStyle() {
        String tagName = getHtmlEl().getTagName();
        if("ul".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.LIST_STYLE_TYPE, "●");
        }else if("ol".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.LIST_STYLE_TYPE, "1");
        }
        this.addStyle(StyleAttr.FONT_SIZE, "12");
    }

    @Override
    public Element build() {
        String listStyleType = getStyleMap().get(StyleAttr.LIST_STYLE_TYPE);
        return getList(listStyleType);
    }

    /**
     * 根据 listStyleType 创建 List 对象
     * @param listStyleType list 的样式类型
     * @return
     */
    private Element getList(String listStyleType) {
        String tagName = getHtmlEl().getTagName();
        if(HtmlTag.UL.equalsIgnoreCase(tagName)){
            return getUlList(listStyleType);
        }else if(HtmlTag.OL.equalsIgnoreCase(tagName)){
            return getOlList(listStyleType);
        }else if(HtmlTag.LI.equalsIgnoreCase(tagName)){
            return getItemList();
        }

        return null;
    }

    private List getOlList(String listStyleType){
        String numberListType = numberTypeMap.get(listStyleType);
        int symbolIndent = 12;
        switch (numberListType){
            case "i" : return new RomanList(false, symbolIndent);
            case "I" : return new RomanList(true, symbolIndent);
            case "a" : {
                List list = new List(false,true, symbolIndent);
                list.setLowercase(true);
                return list;
            }
            case "A" : {
                List list = new List(false,true, symbolIndent);
                list.setLowercase(false);
                return list;
            }
            case "α" : return new GreekList(true, symbolIndent);
            default: return new List(true, symbolIndent);
        }
    }

    private List getUlList(String listStyleType){
        String listType = listTypeMap.get(listStyleType);
        int symbolIndent = 12;
        List list = new List(false,false, symbolIndent);
        list.setListSymbol(new Chunk(listType,getFont()));
        return list;
    }

    private ListItem getItemList(){
        return new ListItem(this.getHtmlEl().getOwnTextString(),getFont());
    }


}
